package tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

import tree.printer.BinaryTreeInfo;

public class BinaryTree<E> implements BinaryTreeInfo{
	
	protected int size;
	protected Node<E> root;

	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	public void clear() {
		size = 0;
		root = null;
	}
	
	/**
	 * 前序遍历
	 * @param visitor
	 */
	public void preorderTraversal(Visitor<E> visitor) {
		if (visitor == null) return;
		preorderTraversal(root, visitor);
	}
	
	private void preorderTraversal(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) return;
		visitor.stop = visitor.visit(node.element);
		preorderTraversal(node.left, visitor);
		preorderTraversal(node.right, visitor);
	}
	
	
	/**
	 * 中序遍历
	 * @param visitor
	 */
	public void inorderTraversal(Visitor<E> visitor) {
		if (visitor == null) return;
		inorderTraversal(root, visitor);
		
	}
	
	private void inorderTraversal(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) return;
		inorderTraversal(node.left, visitor);
		if (visitor.stop) return;
		visitor.stop = visitor.visit(node.element);
		inorderTraversal(node.right, visitor);
	}
	
	
	
	/**
	 * 后序遍历
	 * @param visitor
	 */
	public void postorderTraversal(Visitor<E> visitor) {
		if (visitor == null) return;
		postorderTraversal(root, visitor);
	}
	
	private void postorderTraversal(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) return;
		postorderTraversal(node.left, visitor);
		postorderTraversal(node.right, visitor);
		if (visitor.stop) return;
		visitor.stop = visitor.visit(node.element);
	}
	
	/**
	 * 前序遍历非递归实现
	 */
	public void preorderTraversalByStack(Visitor<E> visitor) {
		if (visitor == null || root == null) return;
		Stack<Node<E>> stack = new Stack<>();
		Node<E> node = root;
		while (true) {
			if (node != null) {
				if (visitor.visit(node.element)) return;
				if (node.right != null) {
					stack.push(node.right);
				}
				node = node.left;
			} else if (stack.isEmpty()) {
				return;
			} else {
				node = stack.pop();
			}
		}
	}
	public void preorderTraversalByStack2(Visitor<E> visitor) {
		if (visitor == null || root == null) return;
		Stack<Node<E>> stack = new Stack<>();
		stack.push(root);
		while (!stack.isEmpty()) {
			Node<E> node = stack.pop();
			if (visitor.visit(node.element)) return;
			if (node.right != null) {
				stack.push(node.right);
			}
			if (node.left != null) {
				stack.push(node.left);
			}
		}
	}
	
	
	/**
	 * 中序遍历非递归实现
	 * @param visitor
	 */
	public void inorderTraversalByStack(Visitor<E> visitor) {
		if (visitor == null || root == null) return;
		Stack<Node<E>> stack = new Stack<>();
		Node<E> node = root;
		while (true) {
			if (node != null) {
				stack.push(node);
				node = node.left;
			} else if (stack.isEmpty()) {
				return;
			} else {
				node = stack.pop();
				if (visitor.visit(node.element)) return;
				node = node.right;
			}
		}
	}
	
	/**
	 * 后序遍历非递归实现
	 * @param visitor
	 */
	public void postorderTraversalByStack(Visitor<E> visitor) {
		if (visitor == null || root == null) return;
		Stack<Node<E>> stack = new Stack<>();
		stack.push(root);
		Node<E> prev = null;
		while (!stack.isEmpty()) {
			Node<E> top = stack.peek();
			if (top.isLeaf() || (prev != null && prev.parent == top)) {
				prev = stack.pop();
				if (visitor.visit(prev.element)) return;
			} else {
				if (top.right != null) {
					stack.push(top.right);
				}
				if (top.left != null) {
					stack.push(top.left);
				}
			}
		}
	}
	
	/**
	 * 获取该二叉树的高度
	 * 递归实现
	 * @return
	 */
	public int height1() {
		return height1(root);
	}
	
	private int height1(Node<E> node) {
		if (node == null) return 0;
		return Math.max(height1(node.left), height1(node.right)) + 1;
	}
	
	public int height2() {
		if (root == null) return 0;
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		int height = 0;
		int levelSize = 1;
		while (!queue.isEmpty()) {
			Node<E> node = queue.poll();
			levelSize--;
			if (node.left != null) {
				queue.offer(node.left);
			}
			if (node.right != null) {
				queue.offer(node.right);
			}
			if (levelSize == 0) {
				height++;
				levelSize = queue.size();
			}
		}
		return height;
	}
	
	/**
	 * 判断该二叉树是否为完全二叉树
	 * @return
	 */
	public boolean isComplete() {
		if (root == null) return false;
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		boolean leaf = false;
		while (!queue.isEmpty()) {
			Node<E> node = queue.poll();
			if (leaf && !node.isLeaf()) {
				return false;
			}
			if (node.left != null) {
				queue.offer(node.left);
			} else if (node.right != null) {
				return false;
			}
			
			if (node.right != null) {
				queue.offer(node.right);
			} else {
				leaf = true;
			}
		}
		return true;
	}
	
	/**
	 * 寻到前驱节点
	 * @param node
	 * @return
	 */
	protected Node<E> predecessor(Node<E> node) {
		if (node == null) return null;
		Node<E> preNode = node.left; 
		if (preNode != null) {
			while (preNode.right != null) {
				preNode = preNode.right;
			}
			return preNode;
		}
		
		while (node.parent != null && node == node.parent.left) {
			node = node.parent;
		}
		return node.parent;
	}
	
	/**
	 * 寻到后继节点
	 * @param node
	 * @return
	 */
	protected Node<E> successor(Node<E> node) {
		if (node == null) return null;
		Node<E> sucNode = node.right;
		if (sucNode != null) {
			while (sucNode.left != null) {
				sucNode = sucNode.left;
			}
			return sucNode;
		}
		while (node.parent != null && node == node.parent.right) {
			node = node.parent;
		}
		return node.parent;
	}
	
	protected void checkElement(E element) {
		if (element == null) throw new NullPointerException("Element must not be null");
	}
	
	public static abstract class Visitor<E> {
		boolean stop;
		public abstract boolean visit(E element);
	}
	
	protected static class Node<E> {
		E element;
		Node<E> parent;
		Node<E> left;
		Node<E> right;
		
		public Node(E element, Node<E> parent) {
			this.element = element;
			this.parent = parent;
		}
		
		public boolean isLeaf() {
			return this.left == null && this.right == null;
		}
		
		public boolean hasTwoChildren() {
			return this.left != null && this.right != null;
		}
		
		public boolean isLeftChild() {
			return parent != null && this == parent.left;
		}
		public boolean isRightChild() {
			return parent != null && this == parent.right;
		}
		
		public Node<E> sibling() {
			if (isLeftChild()) return parent.right;
			if (isRightChild()) return parent.left;
			return null;
		}
		
		@Override
		public String toString() {
			return element.toString();
		}
	}
	
	/**
	 * 以下代码用于打印这棵二叉树
	 */
	@Override
	public Object root() {
		return root;
	}
	@Override
	public Object left(Object node) {
		return ((Node<E>)node).left;
	}
	@Override
	public Object right(Object node) {
		return ((Node<E>)node).right;
	}
	@Override
	public Object string(Object node) {
		return node;
	}
}
